<?php

// $Id: vpage_view_cache_wp.class.php 1570 2009-01-13 12:44:02Z ogrebg $

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class vpage_view_cache_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('category');
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('not_exist');
		$this->add_view($v2);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('vpage', Tangra_Parameter_Method::GET, 'vpage'), 'vpage_event');
	}


	protected function vpage_event($vpage_id) {
		$dbc = $this->get_dbc();

		$vpage = new TCMS_VPage_DBC();
		if ($vpage->load_by_id($dbc, $vpage_id)) {
			if ($vpage->get_cache()) {
				$cache_path = str_replace('/', '-', $vpage->get_path()).'-vp'.$vpage->get_id().'.html';
				$cache_file = $this->get__config()->get_scratch_dir().'tcms_cache/'.$cache_path;
				if(file_exists($cache_file)) {
					$cache = file_get_contents($cache_file);
					$this->export('cache', $cache);
					$view = $this->get_view('default');
				} else {
					$this->export('vpage_cache_missing', $cache_file);
					$view = $this->get_view('default');
				}
			} else {
				$this->export('vpage_cache_off', 1);
				$view = $this->get_view('default');
			}
		} else {
			$this->export('vpage_not_found', 1);
			$view = $this->get_view('default');
		}

		return $view;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting "delete" parameter.');
	}
}